<?php
/**
 * 通用通知接口demo
 * ====================================================
 * 支付完成后，微信会把相关支付和用户信息发送到商户设定的通知URL，
 * 商户接收回调信息后，根据需要设定相应的处理流程。
 * 
 * 这里举例使用log文件形式记录回调信息。
*/
	include_once("./log_.php");
	include_once("../WxPayPubHelper/WxPayPubHelper.php");
	include 'conn_mysql.php';
		//curl请求函数
	function urlPost($curlPost,$url)
	{

          $curl = curl_init();
          curl_setopt($curl, CURLOPT_URL, $url);
          curl_setopt($curl, CURLOPT_HEADER, false);
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($curl, CURLOPT_NOBODY, true);
          curl_setopt($curl, CURLOPT_POST, true);
          curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
          $return_str = curl_exec($curl);
          curl_close($curl);
          return $return_str;
	}


    //使用通用通知接口
	$notify = new Notify_pub();

	//存储微信的回调
	$xml = $GLOBALS['HTTP_RAW_POST_DATA'];	
	$notify->saveData($xml);

	$is_success=0;
	
	//验证签名，并回应微信。
	//对后台通知交互时，如果微信收到商户的应答不是成功或超时，微信认为通知失败，
	//微信会通过一定的策略（如30分钟共8次）定期重新发起通知，
	//尽可能提高通知的成功率，但微信不保证通知最终能成功。
	if($notify->checkSign() == FALSE){
		$notify->setReturnParameter("return_code","FAIL");//返回状态码
		$notify->setReturnParameter("return_msg","签名失败");//返回信息
	}else{
		$is_success=1;
		$notify->setReturnParameter("return_code","SUCCESS");//设置返回码
	}
	$returnXml = $notify->returnXml();
	echo $returnXml;
	
	//==商户根据实际情况设置相应的处理流程，此处仅作举例=======
	$time=date('Y-m-d H:i:s',time());

	$data=$notify->getData();
	$openid=$data['openid'];
	$out_trade_no=$data['out_trade_no'];//订单号
	$total_fee=$data['total_fee']/100;

	$order=explode('P', $out_trade_no);
	$order_id=$order[1];
	
	$sql="INSERT INTO `zhifu_info`(`out_trade_no`, `openid`, `money`, `issuetime`) VALUES ('$out_trade_no','$openid','$total_fee','$time')";
	$query = mysql_query($sql);

	////异步处理订单////

	if($is_success&&$order_id)
	{
       
    $sql="SELECT is_zhifu,pay_points,openid,id_difang,zhifu_monkey FROM `main_user` WHERE  id_self='".$order_id."'";
	$query = mysql_query($sql);
	$row = mysql_fetch_array($query);
	$order_status=$row[0];
	$ke_lvbi=$row[1]?$row[1]:0;
	$openid=$row[2]?$row[2]:0;
	$id_difang=$row[3]?$row[3]:0;
	
	$zhifu_monkey=$row[4]?$row[4]:0;

	$true_money=$zhifu_monkey-$ke_lvbi;
	
	if($true_money==0)$true_money=0.01;


	if($order_status!=1&&$true_money==$total_fee)
	{
		$now_time=time(); 
	    $sql="SELECT user_id,user_name,mobile_phone FROM `ecs_users` WHERE openid='".$openid."'";
		$query = mysql_query($sql);
		$row = mysql_fetch_array($query);
		$user_id=$row[0];
		$user_name=$row[1];
		$mobile_phone=$row[2];

		//获取用户的拥有驴币
		$sql="SELECT pay_points FROM `ecs_users` WHERE user_id='".$user_id."'";
		$query = mysql_query($sql);
		$row = mysql_fetch_array($query);
		$sum_pay_points=$row[0]?$row[0]:0;

		if($sum_pay_points<$ke_lvbi) //驴币是否足够
		{
		    $sql="insert into ecs_account_log(`user_id`,`pay_points`,`change_time`,`change_desc`,`change_type`,`user_money`,`frozen_money`,`rank_points`)  values('".$user_id."','-$ke_lvbi','$now_time','失败，驴币不足，".$user_name."使用驴币支付订单,订单".$order_id."','99',0,0,0)";
		    $query = mysql_query($sql);	
		    exit;
		}

			//扣除相应驴币
		$sql="UPDATE `ecs_users` SET `pay_points`=pay_points-$ke_lvbi where user_id='".$user_id."'";
		$query = mysql_query($sql);


		//记录下驴币使用情况
		// $sql="INSERT INTO `lvbi_shiyong`(`openid`, `num`, `miaoshu`) VALUES ('$openid','$ke_lvbi','代替现金支付订单')";
		// $query = mysql_query($sql);
		//将支付状态设置为已支付
		$sql="UPDATE `main_user` SET `is_zhifu`='1' WHERE openid='".$openid."' and id_self=$order_id";
		$query = mysql_query($sql);
		
		//获取用户应得的驴币id_difang
		$sql="select * from `lvyou_difang_info` where id='$id_difang'";
		$query = mysql_query($sql);
		while($row = mysql_fetch_array($query)){
			$fanduos=$row['fanduos'];
		}
		//记录下驴币使用情况
		// $sql="INSERT INTO `lvbi_huoqv`(`openid`, `huoqv_tujing`, `how_much`, `id_difang`) VALUES ('$openid','支付订单成功获取','$fanduos','$id_difang')";
		// $query = mysql_query($sql);



		//记录下驴币使用情况
		if($ke_lvbi>0)
		{
			$sql="insert into ecs_account_log(`user_id`,`pay_points`,`change_time`,`change_desc`,`change_type`,`user_money`,`frozen_money`,`rank_points`)  values('".$user_id."','-$ke_lvbi','$now_time','".$user_name."使用驴币支付订单,订单".$order_id."','99',0,0,0)";
		    $query = mysql_query($sql);				
		}
        
        //参加活动的人数
		$sql="SELECT count(*) FROM `fu_user` WHERE id='$order_id'";
		$query = mysql_query($sql);
		$row2 = mysql_fetch_array($query);
		$peo_num=$row2[0]?$row2[0]:0;
		$add_pay_points=0;

		

		/*****************zhsk分享赠送驴币**********************/
		$sql="SELECT share_id,status,user_pid FROM `ecs_share_url` WHERE openid='".$openid."' and id_difang='$id_difang'";
	    $query = mysql_query($sql);

	    $row = mysql_fetch_array($query);
	    $share_id=$row[0];
	    $status=$row[1];
	    $user_pid=$row[2];
	    if($status==1)
	    {

		    if($user_pid)
		    {
		    	$sql="SELECT mobile_phone FROM `ecs_users` WHERE user_id='$user_pid'";
				$query = mysql_query($sql);
				$row = mysql_fetch_array($query);
				$p_mobile_phone=$row[0];



				$add_pay_points=$peo_num*10;
				
		    	//日志
				$sql="insert into ecs_account_log(`user_id`,`pay_points`,`change_time`,`change_desc`,`change_type`,`user_money`,`frozen_money`,`rank_points`)  values('$user_pid','$add_pay_points','$now_time','".$user_name."报名，用户分享获得驴币,订单".$order_id."','99',0,0,'$add_pay_points')";
			    $query = mysql_query($sql);
			    //赠送驴币给推荐人
			    $sql="update ecs_users set pay_points=pay_points+".$add_pay_points.",rank_points=rank_points+".$add_pay_points." where user_id='$user_pid'";
			    $query = mysql_query($sql);
			    
			    $sql="UPDATE `ecs_share_url` SET `status`=2 where share_id='$share_id'";
				$query = mysql_query($sql);

		    }    	
	    }
	    /*****************zhsk分享赠送驴币**********************/


		//赠与驴币-订单日志
		if($fanduos>0)
		{
			$sum_fanduos=$peo_num*$fanduos-$add_pay_points;	
			if($sum_fanduos<0)$sum_fanduos=0;
			$sql="insert into ecs_account_log(`user_id`,`pay_points`,`change_time`,`change_desc`,`change_type`,`user_money`,`frozen_money`,`rank_points`)  values('".$user_id."','$sum_fanduos','$now_time','".$user_name."支付订单成功,订单".$order_id."，赠送驴币','99',0,0,'$sum_fanduos')";
	        $query = mysql_query($sql);

	        
	        //给予相应驴币
			$sql="UPDATE `ecs_users` SET `pay_points`=pay_points+$sum_fanduos,`rank_points`=rank_points+$sum_fanduos where user_id='$user_id'";
			$query = mysql_query($sql);		
		}

	  //   //短信通知

	    $target = "http://xtx.telhk.cn:8080/sms.aspx?action=send";
	    

	    if($mobile_phone)
	    {
		    $content="【驴行天下】您的订单已经支付成功，如有需要请拨打057382716699。";
					
	        $post_data ="userid=6792&account=jxlxtx&password=jx72364&mobile=$mobile_phone&content={$content}&sendTime=&extno=";
			
			$httpstr =urlPost($post_data, $target);  	
	    }

	    //分享得驴币——短信通知
	    if($p_mobile_phone)
	    {
		    $content="【驴行天下】恭喜您，推荐".$user_name."报名成功，奖励".$add_pay_points."元驴币，请查看，如有疑问拨打057382716699。";
					
	        $post_data ="userid=6792&account=jxlxtx&password=jx72364&mobile=$p_mobile_phone&content={$content}&sendTime=&extno=";
			
			$httpstr =urlPost($post_data, $target);  	
	    }

	    //给客服发信息
	    $sql="SELECT value FROM `ecs_shop_config` WHERE code='sms_shop_mobile' ";
	    $query = mysql_query($sql);

	    $row = mysql_fetch_array($query);
	    $sms_shop_mobile=$row[0];

	    if($sms_shop_mobile)
	    {
		    $content="【驴行天下】会员".$mobile_phone."的订单已经支付成功，请及时处理。";
					
	        $post_data ="userid=6792&account=jxlxtx&password=jx72364&mobile=$sms_shop_mobile&content={$content}&sendTime=&extno=";
			
			$httpstr =urlPost($post_data, $target);

			
	    }
	 }
	}


	


?>